//3级类目联动，voidarea,20200613
(function ($) {
        $.fn.chainSelect = function (settings) {
            if (this.length < 1) {
                return;
            }
            // 默认值
            settings = $.extend({
                url: "/js/test.js",
                first: null,
                second: null,
                third: null,
                nodata: null,
                required: false,
                onSecondChange: function (code) {
                },
                onThirdChange: function (code) {
                },
                onInitComplete:function () {

                }
            }, settings);

            var box_obj = this;
            var first_obj = box_obj.find(".first");
            var second_obj = box_obj.find(".second");
            var third_obj = box_obj.find(".third");
            var select_prehtml = (settings.required) ? "" : "<option value=''>请选择</option>";

            var findChildNodes = function (parentId, callback) {
                $.ajax(settings.url, {
                    method: 'POST',
                    data: {
                        parentCatId: parentId
                    },
                    dataType: 'json',
                    success: function (res) {
                        if (res.resultCode !== 2000) {
                            return layer.msg('获取类目失败');
                        }
                        var _data = [];
                        for (var i = 0; i < res.data.length; i++) {
                            _data.push({
                                id: res.data[i].catId,
                                name: res.data[i].catName,
                                dataTag: res.data[i].commissionRate
                            });
                        }
                        callback(_data);
                    },
                    error: function () {
                        layer.msg('获取类目');
                    }
                });
            };
            var _hideSecondAndThird = function () {
                if (settings.nodata == "none") {
                    second_obj.css("display", "none");
                    third_obj.css("display", "none");
                } else if (settings.nodata == "hidden") {
                    second_obj.css("visibility", "hidden");
                    third_obj.css("visibility", "hidden");
                }
            };

            var _hideThird = function () {
                if (settings.nodata == "none") {
                    third_obj.css("display", "none");
                } else if (settings.nodata == "hidden") {
                    third_obj.css("visibility", "hidden");
                }
            };

            // 赋值第二级函数
            var secondStart = function (callback) {
                var first_id = first_obj.get(0).value;
                second_obj.empty().attr("disabled", true);
                third_obj.empty().attr("disabled", true);
                second_obj.empty().parent().css({"display": "none"});
                third_obj.empty().parent().css({"display": "none"});
                if (first_id == '' || first_id < 0) {
                    _hideSecondAndThird();
                    return;
                }
                ;
                // 遍历赋值第二级下拉列表
                temp_html = select_prehtml;
                findChildNodes(first_id, function (data) {
                    if (data.length == 0) {
                        _hideSecondAndThird();
                        return;
                    }
                    $.each(data, function (i, second) {
                        temp_html += "<option value='" + second.id + "' data-tag='" + second.dataTag + "'>" + second.name + "</option>";
                    });
                    second_obj.html(temp_html).attr("disabled", false).css({"display": "", "visibility": ""});
                    second_obj.parent().css({display: ""});
                    if (callback) {
                        callback();
                    } else {
                        thirdStart();
                    }
                });
            };
            // 赋值第三级函数
            var thirdStart = function (callback) {
                var first_id = first_obj.get(0).value;
                var second_id = second_obj.get(0).value;
                third_obj.empty().attr("disabled", true);
                if (first_id == '' || first_id < 0 || second_id == '' || second_id < 0) {
                    _hideThird();
                    return;
                }
                // 遍历赋值第三级下拉列表
                temp_html = select_prehtml;

                findChildNodes(second_id, function (data) {
                    if (data.length == 0) {
                        _hideThird();
                        return;
                    }
                    $.each(data, function (i, third) {
                        temp_html += "<option value='" + third.id + "' data-tag='" + third.dataTag + "'>" + third.name + "</option>";
                    });
                    third_obj.html(temp_html).attr("disabled", false).css({"display": "", "visibility": ""});
                    third_obj.parent().css({display: ""});
                    if (callback) {
                        callback();
                    }
                });
            };

            var init = function () {
                second_obj.empty().parent().css({"display": "none"});
                third_obj.empty().parent().css({"display": "none"});
                // 遍历赋值第三级下拉列表
                temp_html = select_prehtml;
                findChildNodes(0, function (data) {
                    $.each(data, function (i, first) {
                        temp_html += "<option value='" + first.id + "' data-tag='" + first.dataTag + "'>" + first.name + "</option>";
                    });
                    first_obj.html(temp_html);
                    // 若有传入第一级与第二级的值，则选中。（setTimeout为兼容IE6而设置）
                    if (settings.first == null) {
                        _listenChange();
                        return;
                    }
                    first_obj.val(settings.first);
                    secondStart(function () {
                        if (settings.second == null) {
                            _listenChange();
                            return;
                        }
                        second_obj.val(settings.second);
                        settings.onSecondChange(settings.second);
                        thirdStart(function () {
                            if (settings.third != null) {
                                third_obj.val(settings.third);
                            }
                            _listenChange();
                        });
                    });

                });
            };

            var _listenChange = function () {
                first_obj.bind("change", function () {
                    secondStart();
                });
                second_obj.bind("change", function () {
                    thirdStart();
                    settings.onSecondChange(second_obj.val());
                });
                third_obj.bind("change", function () {
                    settings.onThirdChange($(third_obj).val());
                });
                settings.onInitComplete();
            };

            $.fn.getSelectData = function (selectNo) {//1,2,3
                var obj = selectNo == 1 ? first_obj : selectNo == 2 ? second_obj : selectNo == 3 ? third_obj : null;
                if (obj == null) {
                    return {id: '', name: '', tag: ''};
                }
                var _option = obj.find("option:selected");
                return {id: _option.val(), name: _option.text(), tag: _option.attr('data-tag')};
            };
            init();
            return this;
        }
    }
)(jQuery);